package controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import domain.Person;
import dto.PersonDTO;
import service.CreditCardService;
import service.PersonService;

@Controller
@RequestMapping("main")
public class MainController {
	@Resource(name = "personService")
	private PersonService personService;

	@Resource(name = "creditCardService")
	private CreditCardService creditCardService;

	private static Logger log = Logger.getLogger(MainController.class);

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getAll(Model model) {
		List<Person> allPersons = personService.getAll();
		List<PersonDTO> personDTOArr = new ArrayList<PersonDTO>();
		for (Person person : allPersons) {
			PersonDTO personDTO = new PersonDTO();
			personDTO.setFirstName(person.getFirstName());
			personDTO.setLastName(person.getLastName());
			personDTO.setId(person.getId());
			personDTO.setAge(person.getAge());
			personDTO.setAddress(person.getAddress());
			personDTO.setCreditCards(creditCardService.getAll(person.getId()));
			personDTOArr.add(personDTO);
		}
		model.addAttribute("allRecords", personDTOArr);
		return "listAll";
	}
}
